package org.example.car.service.controller;

import com.alibaba.excel.EasyExcel;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.example.car.service.domain.Battery;
import org.example.car.service.listener.BatteryListener;
import org.example.car.service.service.BatteryService;

import org.example.car.service.utils.Result;
import org.spring.kun.common.ExcelUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @Author: Yanyanyu
 * @Date: 2024/6/29 10:59
 */
@RequestMapping("battery")
@RestController
@CrossOrigin
public class BatteryController {

    @Autowired
    private BatteryService batteryService;

    @RequestMapping("list")
    public Result list(@RequestBody Battery battery,Integer pageNum){
        PageHelper.startPage(pageNum,5);
        List<Battery> list=batteryService.getList(battery);
        return Result.success(new PageInfo<>(list));
    }

    @RequestMapping("add")
    public Result add(@RequestBody Battery battery){
        boolean b=batteryService.add(battery);
        return b?Result.success():Result.fail();
    }

    @RequestMapping("update")
    public Result update(@RequestBody Battery battery){
        boolean b=batteryService.update(battery);
        return b?Result.success():Result.fail();
    }

    @RequestMapping("del")
    public Result del(Integer batteryId){
        boolean b=batteryService.del(batteryId);
        return b?Result.success():Result.fail();
    }

    @RequestMapping("outExcel")
    public void outExcel(HttpServletResponse response){
        List<Battery> list = batteryService.getList(null);
        ExcelUtils.exportExcel(list,"电池厂商","sheet",response);
    }

    @RequestMapping("readExcel")
    public void readExcel(MultipartFile file) throws IOException {
        EasyExcel.read(file.getInputStream(), Battery.class,new BatteryListener(batteryService)).sheet().doRead();
    }

}
